-- Slash command register functions
-- Its not recomendet, too call this functions directly
--[[ >> MyAddon 
embed = true
	MyAddon:RegisterSlashTable(table)
	MyAddon:RegisterSlashFunction(slashName, slashFunc)
	MyAddon.RunSlashCommand(





]]--

local ModuleName, Version = "slashCommand", 1
local ALLib = LibStub and LibStub("ALLib-1.0", true)
if not AceGUI or (ALLib:GetLibModuleVersion(moduleName)  or 0) >= Version then return end

ALLib.libmodules.slash = ALLib.libmodules.slash or {}						-- lib module table
ALLib.libmodules.slashFunctions = ALLib.libmodules.slashFunctions or {}		-- Contains all slash functions

local Slash = ALLib.libmodules.slash

-- splits and insert the slashtable into the slashcommands
local function insertSlashTable(mainTable, table)
	for k,v in pairs(table) do
		if type(v) == "function" then
			mainTable = v
		elseif type(v) == "table" then
			mainTable[k] = {}
			insertSlashTable(mainTable[k], v)
		end
	end
end

-- search and run a slash function
local function runSlashFunc(table)
	
end

--- Registers a table with slashcommands
-- @param table <table> Table with slash info
-- @param addonName <string> Name of the addon
-- @usage 
-- -- Addon usage 'MyAddon:RegisterSlashTable(table)'
-- :RegisterSlashTable({
-- 		["myaddon"] = {							-- /myaddon
--			["test"] = function(...)			-- /myaddon test
--				print(...)
--			end,
--			["test2"] = {						-- /myaddon test2
--				["test3"] = function(a,b,c)		-- /myaddon test2 test3
--					print(a, b, c)
--				end,
--			},
--			["nil"] = function(...)					-- this is called if nothing is found 
--				print("ERROR")
--			end,
--		},
-- })
-- /myaddon test HELLO WORLD
--		-> print: HELLO WORLD
--
-- /myaddon test2 test3 TEST1 TEST2 TEST3
--		-> print: TEST1 TEST2 TEST3
--
-- /myaddon lol
-- 		-> print: ERROR
function Slash:RegisterSlashTable(table, addonName)
	addonName = addonName or self.name
	if type(table) ~= "table" then
		error(("Usage: RegisterSlashTable(table[, addonName]): 'table' - table expected got '%s'."):format(type(table)), 2)
	end
	if not ALLib:GetAddon(addonName) then
		error(("Usage: RegisterSlashTable(table[, addonName]): 'addonName' - Cannot find addon '%s'."):format(tostring(addonName)), 2)
	end
	
	if not ALLib.libmodules.slashFunctions[addonName] then ALLib.libmodules.slashFunctions[addonName] = {} end
	
	for slashName,slashTable in pairs(table) do
		if slashName ~= "nil" then
			if not ALLib.libmodules.slashFunctions[addonName][slashName] then ALLib.libmodules.slashFunctions[addonName][slashName] = {} end
			table.insert(Command.Slash.Register(slashName), {function(arg) Slash.RunSlashCommand(nil, slashName, arg) end, addonName, "SlashCommand"})
			insertSlashTable(ALLib.libmodules.slashFunctions[addonName][slashName], slashTable)
		end
	end
end

--- Registers a slashcommand with a function
-- @param slashName <string> Name of the slash command ( /name )
-- @param slashFunc <function> The function that the slash command run
-- @param addonName <string> Name of the addon
function Slash:RegisterSlashFunction(slashName, slashFunc, addonName)
	addonName = addonName or self.name
	if type(slashName) ~= "string" then
		error(("Usage: RegisterSlashFunction(slashName, slashFunc[, addonName]): 'string' - slashName expected got '%s'."):format(type(table)), 2)
	end
	if type(table) ~= "function" then
		error(("Usage: RegisterSlashTable(slashName, slashFunc[, addonName]): 'function' - slashFunc expected got '%s'."):format(type(table)), 2)
	end
	if not ALLib:GetAddon(addonName) then
		error(("Usage: RegisterSlashTable(slashName, slashFunc[, addonName]): 'addonName' - Cannot find addon '%s'."):format(tostring(addonName)), 2)
	end
	
	ALLib.libmodules.slashFunctions[addonName][slashName] = slashFunc
	
	table.insert(Command.Slash.Register(slashName), {function(arg) Slash.RunSlashCommand(slashName, arg) end, addonName, "SlashCommand"})
	
end
--table.insert(Event.Addon.SavedVariables.Save.Begin, {function () noaxsave() end, "NoaxVariableTest", "Save variables"})
--table.insert(Event.Addon.SavedVariables.Load.Begin, {function () noaxload() end, "NoaxVariableTest", "Load variables"})

--table.insert(Command.Slash.Register("noaxadd"), {function () noaxadd() end, "NoaxVariableTest", "Slash command"})
--table.insert(Command.Slash.Register("noaxdisplay"), {function () noaxdisplay() end, "NoaxVariableTest", "Slash command"})
--/run for k,v in string.gmatch("lol1 lol2 3 lol4", "([%w%d]+)") do print(k, " = ", v, " -> ", type(k)) end

--- Runs a slash command
-- This can run registered and other slash commands from addons that are not using ALLib

function Slash:RunSlashCommand(slashName, arg)
	
end

local funcTableEmbeds = {
	"RegisterSlashTable",
	"RegisterSlashFunction",
	"RunSlashCommand",
}

function Slash:Load(target, embed)
	if embed then
		for k,v in ipairs(funcTableEmbeds) do
			target[v] = self[v]
		end
	else
		target.Slash = target.Slash or {}
		for k,v in ipairs(funcTableEmbeds) do
			target.Slash[v] = self[v]
		end
	end
	return target
	
end

ALLib:RegisterLibModule(ModuleName, Version)